<?php
class Model_Table_category extends Zend_Db_Table{
	
	protected $_name = 'category';
	protected $_primary = 'id';
	
	public function allItems($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()->from('category')->order('order ASC');
		$result = $db->fetchAll($select);
		$data = new Zendvn_Recursive($result);
		$result = $data->process(0);
		return $result;
	}
	public function getItem($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()->from('category')->where('id =' . $arrParam['id']);
		return $db->fetchRow($select);
	}
	public function getItemWhere($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()->from('category')->where('parent =' . $arrParam['parent'])->where('name =?',$arrParam['name'],3);
		if($options == 'edit'){
			$select = $db->select()->from('category')->where('parent =' . $arrParam['parent'])
														->where('id != ?',$arrParam['id'],1)
														->where('name =?',$arrParam['name'],3);
		}
		echo '</br>' . $select;
		return $db->fetchAll($select);
	}
	public function getLevel1($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()->from('category')->where('parent = 0')
								->where('status = 1')->order('order ASC');
		$result = $db->fetchAll($select);
		return $result;
	}
	public function getLevel2($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()->from('category')->where('parent != 0')
								->where('status = 1')->order('name ASC');
		$result = $db->fetchAll($select);
		return $result;
	}
	public function  addItem($arrParam = null,$options = null){
		if($arrParam['radio'] == 0){
			$parent = 0;
		}else{
			$parent = $arrParam['parent'];
		}		
		$row = $this->fetchNew();
		$row->name = $arrParam['name'];
		$row->parent = $parent;
		$row->order = $arrParam['order'];
		$row->status = $arrParam['status'];
		$row->save();
	}
	public function deleteItem($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$ids[] = $arrParam['id'];
		$select = $db->select()->from('category')->where('parent = ' . $arrParam['id']);
		$result = $db->fetchAll($select);
		if(count($result) > 0){
			foreach ($result as $val){
				$ids[] = $val['id'];
			}
		}
		foreach ($ids as $val){
			//Tim cac san pham thuoc category can xoa
			
			//Xoa cac san pham do
				
			//Xoa category do di
			$where = 'id =' . $val;
			$this->delete($where);
		}		
	}
	public function editItem($arrParam = null,$options = null){
		$where = 'id =' . (int)$arrParam['id'];
		$row = $this->fetchRow($where);
		$row->name = $arrParam['name'];
		if($arrParam['parent'] != null){
			$row->parent = $arrParam['parent'];
		}
		$row->order = $arrParam['order'];
		$row->status = $arrParam['status'];
		$row->save();
	}
	public function changeStatus($arrParam = null,$options = null){
		$status = $arrParam['status'];
		$where = 'id =' . (int)$arrParam['id'];
		$row = $this->fetchRow($where);
		if($status == 1) $status = 0;
		else if($status ==0) $status =1;
		$row->status = $status;
		$row->save();
		return $status;
	}
}